import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../../dd_core/dd_config.dart';
import '../../../dd_core/dd_hive_dao.dart';
import '../../../dd_core/dd_http.dart';
import '../../../dd_models/dd_hive_works_model.dart';

class DDBlockListController extends GetxController {
  final datas = <BlockUser>[].obs;

  @override
  onInit() {
    super.onInit();
    getList();
  }

  getList() async {
    EasyLoading.show();
    Map<String, dynamic>? result = await blockList(40, 0);
    EasyLoading.dismiss();
    if (result != null && result['code'] == 0 && result['data'] != null) {
      List<Map<String, dynamic>> data =
          List<Map<String, dynamic>>.from(result['data']);
      datas.value = data.map((e) => BlockUser.fromJson(e)).toList();
    }
  }

  updateMyBlock(BlockUser m) async {
    EasyLoading.show();
    Map<String, dynamic>? result = await removeBlock(m.broadcasterId ?? '');
    EasyLoading.dismiss();
    if (result != null && result['code'] == 0) {
      HiveWorksModel hiveWorksModel = HiveWorksModel();
      hiveWorksModel.userId = m.broadcasterId;
      await DDHiveDao.updateFencingBlock(
          hiveWorksModel: hiveWorksModel, myBlock: false);
      getList();
    }
  }

  @override
  void onClose() {
    datas.close();
    super.onClose();
  }
}

class BlockUser {
  final String nickName;
  final String avatar;
  final int age;
  final String registerCountry;
  final String broadcasterId;
  final int gender;

  BlockUser({
    required this.nickName,
    required this.avatar,
    required this.age,
    required this.registerCountry,
    required this.broadcasterId,
    required this.gender,
  });

  factory BlockUser.fromJson(Map<String, dynamic> json) {
    return BlockUser(
      nickName: json['nickName'],
      avatar: json['avatar'],
      age: json['age'],
      registerCountry: json['registerCountry'],
      broadcasterId: json['broadcasterId'],
      gender: json['gender'],
    );
  }
}
